/**
 * 研发项目分支业务逻辑
 */
// 直接导入相关模块
const ProjectHelpers = require('../../../config/project_helpers_config');
const cloudHelper = ProjectHelpers.cloudHelper;
const pageHelper = ProjectHelpers.pageHelper;
const ResearchProjectModel = require('../../model/project_model.js');
const ResearchProjectFlowModel = require('../../model/project_flow_model.js');

/**
 * 项目分支业务逻辑
 */
const ProjectBranchBiz = {

	/**
	 * 获取项目分支列表
	 * @param {String} projectId 项目ID
	 * @returns {Promise} 异步结果
	 */
	async getProjectBranchList(projectId) {
		if (!projectId) return { list: [], total: 0 };
		
		try {
			const params = {
				parentId: projectId,
				page: 1,
				size: 20
			};
			
			const result = await cloudHelper.callCloudData('research_project/branch_list', params);
			if (!result) return { list: [], total: 0 };
			
			return result;
		} catch (err) {
			console.error('[研发项目] 获取项目分支列表失败:', err);
			return { list: [], total: 0 };
		}
	},

	/**
	 * 获取父项目
	 * @param {String} projectId 项目ID
	 * @returns {Promise} 异步结果
	 */
	async getParentProject(projectId) {
		if (!projectId) return null;
		
		try {
			const params = {
				id: projectId,
				page: 1,
				size: 1
			};
			
			const result = await cloudHelper.callCloudData('research_project/parent', params);
			
			// 检查父项目ID是否与当前项目ID相同(防止自引用)
			if (result && result._id === projectId) {
				console.warn('[研发项目] 父项目ID与当前项目ID相同，疑似数据错误，返回null');
				return null;
			}
			
			return result;
		} catch (err) {
			console.error('[研发项目] 获取父项目失败:', err);
			return null;
		}
	},

	/**
	 * 创建项目分支
	 * @param {Object} projectData 项目数据
	 * @returns {Promise} 异步结果
	 */
	async createProjectBranch(projectData) {
		if (!projectData) return null;
		
		try {
			return await cloudHelper.callCloudData('research_project/branch_create', projectData);
		} catch (err) {
			console.error('创建项目分支失败', err);
			return null;
		}
	},

	/**
	 * 获取项目分支树
	 * @param {String} projectId 项目ID
	 * @returns {Promise} 异步结果
	 */
	async getProjectBranchTree(projectId) {
		if (!projectId) return null;
		
		try {
			const params = {
				id: projectId
			};
			
			return await cloudHelper.callCloudData('research_project/branch_tree', params);
		} catch (err) {
			console.error('获取项目分支树失败', err);
			return null;
		}
	},

	/**
	 * 检查是否可以创建分支
	 * @param {Object} parentProject 父项目
	 * @returns {Boolean} 是否可以创建分支
	 */
	canCreateBranch(parentProject) {
		if (!parentProject) return false;
		
		// 只有活动中的项目可以创建分支
		return parentProject.PROJECT_STATUS === ResearchProjectModel.STATUS.ACTIVE;
	},

	/**
	 * 获取项目详情
	 * @param {string} projectId 项目ID
	 * @returns {Promise} 异步结果
	 */
	async _getProjectDetail(projectId) {
		try {
			const params = {
				id: projectId
			};
			
			const result = await cloudHelper.callCloudData('research_project/detail', params);
			return result;
		} catch (err) {
			console.error('[ProjectBranchBiz]获取项目详情失败', err);
			return null;
		}
	},

	/**
	 * 记录分支创建流转记录
	 * @param {string} parentId 父项目ID
	 * @param {string} branchId 分支项目ID
	 * @param {string} branchNodeId 分支节点ID
	 * @returns {Promise} 异步结果
	 */
	async _recordBranchFlow(parentId, branchId, branchNodeId) {
		try {
			// 获取父项目信息
			const parentProject = await this._getProjectDetail(parentId);
			if (!parentProject) {
				throw new Error('父项目不存在');
			}
			
			// 获取分支项目信息
			const branchProject = await this._getProjectDetail(branchId);
			if (!branchProject) {
				throw new Error('分支项目不存在');
			}
			
			// 准备父项目流转数据
			const parentFlowData = {
				id: parentId,
				status: parentProject.PROJECT_STATUS,
				activeStatus: parentProject.ACTIVE_STATUS,
				FLOW_TYPE: ResearchProjectFlowModel.FLOW_TYPE.BRANCH,
				FLOW_NOTE: `创建分支项目: ${branchProject.PROJECT_NAME}`,
				TAGS: [{
					TAG_ID: branchId,
					TAG_NAME: branchProject.PROJECT_NAME
				}]
			};
			
			// 准备分支项目流转数据
			const branchFlowData = {
				id: branchId,
				status: branchProject.PROJECT_STATUS,
				activeStatus: branchProject.ACTIVE_STATUS,
				FLOW_TYPE: ResearchProjectFlowModel.FLOW_TYPE.BRANCH,
				FLOW_NOTE: `从项目 ${parentProject.PROJECT_NAME} 创建分支`,
				TAGS: [{
					TAG_ID: parentId,
					TAG_NAME: parentProject.PROJECT_NAME
				}]
			};
			
			// 记录父项目流转
			await cloudHelper.callCloudSumbit('research_project/status', parentFlowData);
			
			// 记录分支项目流转
			await cloudHelper.callCloudSumbit('research_project/status', branchFlowData);
			
			return true;
		} catch (err) {
			console.error('[ProjectBranchBiz]记录分支创建流转记录失败', err);
			return false;
		}
	}
};

module.exports = ProjectBranchBiz; 